#include "nvs.h"
#include "nvs_flash.h"
nvs_handle_t nvsHandle;

void nvsInit(void)
{
    nvs_flash_init();    
}

bool nvsRead_i8(const char* storageNameSpace, const char* dataNameSpace, int8_t* out_value)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_i8(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsRead_u8(const char* storageNameSpace, const char* dataNameSpace, uint8_t* out_value)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_u8(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsRead_i16(const char* storageNameSpace, const char* dataNameSpace, int16_t* out_value)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_i16(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsRead_u16(const char* storageNameSpace, const char* dataNameSpace, uint16_t* out_value)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_u16(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsRead_i32(const char* storageNameSpace, const char* dataNameSpace, int32_t* out_value)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_i32(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsRead_u32(const char* storageNameSpace, const char* dataNameSpace, uint32_t* out_value)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_u32(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsRead_i64(const char* storageNameSpace, const char* dataNameSpace, int64_t* out_value)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_i64(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsRead_u64(const char* storageNameSpace, const char* dataNameSpace, uint64_t* out_value)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_u64(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsRead_str(const char* storageNameSpace, const char* dataNameSpace, char* out_value, size_t* len)
{
    if(nvs_open(storageNameSpace, NVS_READONLY, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_str(nvsHandle, dataNameSpace, out_value, len) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}



bool nvsWrite_i8(const char* storageNameSpace, const char* dataNameSpace, int8_t out_value)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_set_i8(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsWrite_u8(const char* storageNameSpace, const char* dataNameSpace, uint8_t out_value)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_set_u8(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsWrite_i16(const char* storageNameSpace, const char* dataNameSpace, int16_t out_value)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_set_i16(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsWrite_u16(const char* storageNameSpace, const char* dataNameSpace, uint16_t out_value)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_set_u16(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsWrite_i32(const char* storageNameSpace, const char* dataNameSpace, int32_t out_value)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_set_i32(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsWrite_u32(const char* storageNameSpace, const char* dataNameSpace, uint32_t out_value)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_set_u32(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsWrite_i64(const char* storageNameSpace, const char* dataNameSpace, int64_t out_value)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_set_i64(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsWrite_u64(const char* storageNameSpace, const char* dataNameSpace, uint64_t out_value)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_set_u64(nvsHandle, dataNameSpace, out_value) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}

bool nvsWrite_str(const char* storageNameSpace, const char* dataNameSpace, char* out_value, size_t* len)
{
    if(nvs_open(storageNameSpace, NVS_READWRITE, &nvsHandle) == ESP_OK)
    {

        if(nvs_get_str(nvsHandle, dataNameSpace, out_value, len) != ESP_OK)
        {
            return 0;
        }
        nvs_close(nvsHandle);
        return 1;
    }
    return 0;
}